﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Net;
using System.Net.Sockets;
using Sharebert.BootstrapServer.API;
using Sharebert.LinkLayerLibrary.API;
using Sharebert.LinkLayerLibrary;
using Sharebert.OverlayLibrary.Messages;

namespace Sharebert.BootstrapServer
{
    /// <summary>
    /// ClientHandler provides an easy to use interface to reply to received messages.
    /// </summary>
    public class ClientHandler : IClientHandler
    {

        /// <summary>
        /// Initializes a new instance of the ClientHandler class.
        /// </summary>
        /// <param name="conn"></param>
        public ClientHandler(IConnection conn)
        {
            if (conn == null)
            {
                throw new ArgumentNullException("Connection can not be null");
            }
            this.connection = conn;
        }

        /// <summary>
        /// Sends the predecessor and successor to the requesting new peer.
        /// </summary>
        /// <param name="neighbours">The predecessor and successor of the peer</param>
        /// <exception cref="NetworkException">Could not send message</exception>
        public void SendNeighbours(NeighbourList neighbours)
        {
            var payload = Serialisation.Serialize(neighbours);
            var header = new Header(MessageTypes.NEIGHBOUR_RESPONSE, payload.Length);
            var message = new Message(header, payload);            
            connection.SendMessage(message);
        }

        /// <summary>
        /// Sends an information to the peer, that the PeerID is already in use.
        /// </summary>
        /// <exception cref="NetworkException">Could not send message</exception>
        public void SendPeerIdInUse()
        {
            var payload = new byte[0];
            var header = new Header(MessageTypes.PEERID_IN_USE, payload.Length);
            var message = new Message(header, payload);
            connection.SendMessage(message);
        }

        /// <summary>
        /// Closes the underlying network stream and socket.
        /// </summary>
        public void Disconnect()
        {
            this.connection.Close();
        }

        /// <summary>
        /// Returns the remote [IP]:[Port]
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.RemoteIP + ":" + this.RemotePort;
        }


        #region Properties and private member

        public string RemoteIP { get { return this.connection.RemoteAddress; } }

        public int RemotePort { get { return this.connection.RemotePort; } }

        public string LocalIP { get { return this.connection.LocalAddress; } }

        public int LocalPort { get { return this.connection.LocalPort; } }

        private IConnection connection;

        #endregion

    }
}
